﻿// See https://aka.ms/new-console-template for more information

using System.Collections;
using System.Runtime.CompilerServices;

MyClass mc = new MyClass();
//接口显示实现
((IFoo)mc).Foo();

//延迟等待两秒
//MyDelay md = new MyDelay(2);
//await md;
//Console.WriteLine("等待完毕");
//await TimeSpan.FromSeconds(1);
//Console.WriteLine("等待完毕");
//await 1;
//Console.WriteLine("等待完毕");

MyClass2 mc2 = new MyClass2();
foreach (var itme in mc2)
{
    Console.WriteLine(itme);
}

foreach(var itme in 10)
{
    Console.WriteLine(itme);
}

Console.WriteLine("Hello, World!");

//接口显示实现,接口阉割
internal class MyClass : IFoo
{
    void IFoo.Foo()
    {
        Console.WriteLine("显示实现方法");
    }
}

internal interface IFoo
{
    void Foo();
}

//可等待
//任何提供了GetAwaiter()方法的类都可以await
internal class MyDelay
{
    public int seconds { get; private set; }

    public MyDelay(int seconds)
    {
        this.seconds = seconds;
    }

    //实现GetAwaiter方法，可用扩展方法的方式实现
    //public TaskAwaiter GetAwaiter()
    //{
    //    return Task.Delay(TimeSpan.FromSeconds(seconds)).GetAwaiter();
    //}
}

internal static class MyExtensions
{
    public static TaskAwaiter GetAwaiter(this MyDelay md)
    {
        return Task.Delay(TimeSpan.FromSeconds(md.seconds)).GetAwaiter();
    }

    public static TaskAwaiter GetAwaiter(this TimeSpan md)
    {
        return Task.Delay(md).GetAwaiter();
    }

    public static TaskAwaiter GetAwaiter(this int md)
    {
        return Task.Delay(TimeSpan.FromSeconds(md)).GetAwaiter();
    }
}

//可枚举
//当一个类实现了IEnumerable接口就可以进行foreach操作
//只需实现public IEnumerator GetEnumerator()方法也可foreach
//IEnumerable
internal class MyClass2
{
    public IEnumerator GetEnumerator()
    {
        for (var i = 0; i < 10; i++)
        {
            yield return i.ToString();
        }
    }
}

internal static class MyExtensions2
{
    public static IEnumerator GetEnumerator(this int I)
    {
        for (var i = 0; i < I; i++)
        {
            yield return i.ToString();
        }
    }
}

//异步可枚举